#!/bin/bash
# Author    : some-guy (Muhammed U)
# Copyright : (C) 2008 some-guy
# E-mail    : muhammedu (at) gmail (dot) com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# Requires libslaxget

## Download a file
# $1 == File URL
# $2 == append / output / stdout(verbose)
# $3 == Mode: quiet/verbose
slapt_retrieve_file()
{
FILE_URL=$1
FILE_APPEND=$2
FILE_DOWN_MANAGER=$3
[ $FILE_APPEND == "append" -o $FILE_APPEND == "output" ] || slax_bail "$0: $FILE_APPEND is not a valid choice"
if [ $FILE_DOWN_MANAGER == "verbose" ]; then
	if [ $2 != stdout ]; then
		curl --fail --progress --$FILE_APPEND `echo $FILE_URL | sed 's,.*/,,'` $FILE_URL
	else
		curl --fail --silent $FILE_URL
	fi
elif [ $FILE_DOWN_MANAGER == "quiet" ]; then
	[ $2 == stdout ] || curl --fail --$FILE_APPEND `echo $FILE_URL | sed 's,.*/,,'` $FILE_URL &>/dev/null
fi
[ $? == 0 ] || exit 5
return 0
}

## Refreshes the repository
# $1 == Repository URL
# $2 == Cache Dir
slapt_refresh_repository()
{
OLDDIR=$PWD
REPO_URL=$1
STD_CACHE=$2
REPO_CACHE="$STD_CACHE/repositories/`echo $REPO_URL | sed -e s,http://,, -e s,ftp://,,`"
[ -d $REPO_CACHE ] || mkdir -p $REPO_CACHE
cd $REPO_CACHE
[ -z "`ls -1`" ] || rm -f ./*
slax_print "Retrieving package data for $1"
for datafile in PACKAGES.TXT CHECKSUMS.md5; do
	slapt_retrieve_file $1/$datafile output quiet
	[ $? == "5" ] && slax_bail "Not a valid repository"
done
slapt_retrieve_file $1/FILELIST.TXT output quiet
cd $OLDDIR
}

## Get a package section of a file
# $1 == Package
# $2 == Cache
# $3 == Config file
# $4 == loose/strict
slapt_get_package()
{
OLDDIR=$PWD
APP=$1
CACHE_DIR=$2
CONFIG_FILE=$3
SEARCH_TYPE=$4
REPO_DIRS_TO_SEARCH=`grep -i SOURCE $CONFIG_FILE | sed 's,SOURCE=,,' | sed -e s,http://,, -e s,ftp://,,`
cd $CACHE_DIR/repositories
for dirtosearch in `echo $REPO_DIRS_TO_SEARCH`; do
	if [ $SEARCH_TYPE == "loose" ]; then
		PACKAGE_LIST_FOR_LOOP=`cat $dirtosearch/PACKAGES.TXT | grep -in $APP | grep "PACKAGE NAME:" | sed 's, ,@,g'`
	else
		PACKAGE_LIST_FOR_LOOP=`cat $dirtosearch/PACKAGES.TXT | grep -in $APP | grep "PACKAGE NAME:" | grep -iw $APP | grep -i "$APP-[0-9]\|$APP-SVN\|$APP-CVS\|$APP-BZR\|$APP-GIT" | sed 's, ,@,g'`
	fi
	for package in $PACKAGE_LIST_FOR_LOOP; do
		package=`echo $package | sed 's,@, ,g'`
		PKG_NAME=`echo $package |  sed -e s,.tgz,, -e s,.*:,, -e s,\ ,,g -e s,-[0-9].[0-9].*,, -e s,-svn.*,,i`
		END_LINE=`cat $dirtosearch/PACKAGES.TXT | grep -in $PKG_NAME: | cut -d: -f1 | tail -1`
		START_LINE=`echo $package | cut -d: -f1`
		perl -ne "print $_ if ($START_LINE..$END_LINE);" $dirtosearch/PACKAGES.TXT
		echo
## Alternative method via awk/gawk
#		for linenumber in `seq $START_LINE $END_LINE 2>/dev/null`; do
#			awk "FNR == $linenumber" $dirtosearch/PACKAGES.TXT
#		done
	done
done
cd $OLDDIR
}

## Search for a app
# $1 == App
# $2 == Cache Dir
# $3 == Config file
slapt_search_app()
{
OLDDIR=$PWD
APP=$1
CACHE_DIR=$2
CONFIG_FILE=$3
REPO_DIRS_TO_SEARCH=`grep -i SOURCE $CONFIG_FILE | sed 's,SOURCE=,,' | sed -e s,http://,, -e s,ftp://,,`
cd $CACHE_DIR/repositories
for dirtosearch in `echo $REPO_DIRS_TO_SEARCH`; do
	for package in `cat $dirtosearch/PACKAGES.TXT | grep -i $APP | grep "PACKAGE NAME:" |  sed -e s,.tgz,, -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,-[0-9].[0-9].*,, -e s,-svn.*,,i`; do
		echo PKGNAME$package
		echo PKGSUMM`cat $dirtosearch/PACKAGES.TXT | grep -i "$package: $package" |  sed -e s,$package:\ ,,`
		echo
	done
done
cd $OLDDIR
}

## Resolve local dependency
# $1 == Dependency
# $2 == Packages data dir
slapt_resolve_local()
{
OLDDIR=$PWD
DEP=$1
DATA_DIR=$2
if [ -z "`echo $DEP | grep \">=\"`" ]; then
	if [ -z "`ls -1 $DATA_DIR | grep -iw $(echo $DEP | sed 's,|.*,,')`" -a -z "`ls -1 $DATA_DIR | grep -iw $(echo $DEP | sed 's,.*|,,')`" ]; then
		echo "$DEP:unresolved"
	else
		echo "$DEP:resolved"
	fi
else
	if [ -z "`ls -1 $DATA_DIR | grep -iw $(echo $DEP | sed 's|>=.*||' | sed 's,|.*,,')`" -a -z "`ls -1 $DATA_DIR | grep -iw $(echo $DEP | sed 's|>=.*||' | sed 's,.*|,,')`" ]; then
		echo "$DEP:unresolved"
	else
		if [ $(echo `ls -1 $DATA_DIR | grep -iw $(echo $DEP | sed 's,|.*,,' | sed 's|>=.*||') | sed -e s,$(echo $DEP | sed 's,|.*,,' | sed 's|>=.*||' | sed 's| ||g')-,, | cut -d"-" -f1` `echo $DEP | sed 's,|.*,,' | cut -d" " -f3 | cut -d"-" -f1` | awk '{print ($1 >= $2) ? "0" : "1" }') == "0" ] || [ $(echo `ls -1 $DATA_DIR | grep -iw $(echo $DEP | sed 's,.*|,,' | sed 's|>=.*||') | sed -e s,$(echo $DEP | sed 's,.*|,,' | sed 's|>=.*||' | sed 's| ||g')-,, | cut -d"-" -f1` `echo $DEP | sed 's,.*| ,,' | cut -d" " -f3 | cut -d"-" -f1` | awk '{print ($1 >= $2) ? "0" : "1" }') == "0" ]; then
			echo "$DEP:resolved"
		else
			echo "$DEP:unresolved"
		fi
	fi
fi
}

### Resolve internet dependency
## $1 == Dependency
## $2 == Cache dir
#slapt_resolve_internet()
#{
#OLDDIR=$PWD
#DEP=$1
#CACHE_DIR=$2
#if [ -z "`echo $DEP | grep \">=\"`" ]; then
#	if [ -z "`(for packagesdir in $(find $CACHE_DIR/repositories -type f | grep PACKAGES.TXT | sed 's|/PACKAGES.TXT||'); do cat $packagesdir/PACKAGES.TXT | grep PACKAGE\ NAME: | sed -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,.tgz,,; done) | grep -iw $(echo $DEP | sed 's,|.*,,')`" -a -z "`(for packagesdir in $(find $CACHE_DIR/repositories -type f | grep PACKAGES.TXT | sed 's|/PACKAGES.TXT||'); do cat $packagesdir/PACKAGES.TXT | grep PACKAGE\ NAME: | sed -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,.tgz,,; done) | grep -iw $(echo $DEP | sed 's,.*|,,')`" ]; then
#		echo "$DEP:unresolved"
#	else
#		echo "$DEP:resolved"
#	fi
#else
#	if [ -z "`(for packagesdir in $(find $CACHE_DIR/repositories -type f | grep PACKAGES.TXT | sed 's|/PACKAGES.TXT||'); do cat $packagesdir/PACKAGES.TXT | grep PACKAGE\ NAME: | sed -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,.tgz,,; done) | grep -iw $(echo $DEP | sed 's|>=.*||' | sed 's,|.*,,')`" -a -z "`(for packagesdir in $(find $CACHE_DIR/repositories -type f | grep PACKAGES.TXT | sed 's|/PACKAGES.TXT||'); do cat $packagesdir/PACKAGES.TXT | grep PACKAGE\ NAME: | sed -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,.tgz,,; done) | grep -iw $(echo $DEP | sed 's|>=.*||' | sed 's,.*|,,')`" ]; then
#		echo "$DEP:unresolved"
#	else
#		if [ $(echo `(for packagesdir in $(find $CACHE_DIR/repositories -type f | grep PACKAGES.TXT | sed 's|/PACKAGES.TXT||'); do cat $packagesdir/PACKAGES.TXT | grep PACKAGE\ NAME: | sed -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,.tgz,,; done) | grep -iw $(echo $DEP | sed 's,|.*,,' | sed 's|>=.*||') | sed -e s,$(echo $DEP | sed 's,|.*,,' | sed 's|>=.*||' | sed 's| ||g')-,, | cut -d"-" -f1` `echo $DEP | sed 's,|.*,,' | cut -d" " -f3 | cut -d"-" -f1` | awk '{print ($1 >= $2) ? "0" : "1" }') == "0" ] || [ $(echo `(for packagesdir in $(find $CACHE_DIR/repositories -type f | grep PACKAGES.TXT | sed 's|/PACKAGES.TXT||'); do cat $packagesdir/PACKAGES.TXT | grep PACKAGE\ NAME: | sed -e s,PACKAGE\ NAME:,, -e s,\ ,,g -e s,.tgz,,; done) | grep -iw $(echo $DEP | sed 's,.*|,,' | sed 's|>=.*||') | sed -e s,$(echo $DEP | sed 's,.*|,,' | sed 's|>=.*||' | sed 's| ||g')-,, | cut -d"-" -f1` `echo $DEP | sed 's,.*| ,,' | cut -d" " -f3 | cut -d"-" -f1` | awk '{print ($1 >= $2) ? "0" : "1" }') == "0" ]; then
#			echo "$DEP:resolved"
#		else
#			echo "$DEP:unresolved"
#		fi
#	fi
#fi
#}

## Resolve internet dependency
# $1 == Dependency
# $2 == Cache dir
slapt_resolve_internet()
{
OLDDIR=$PWD
DEP=$1
DATA_DIR=$2
if [ -z "`echo $DEP | grep \">=\"`" ]; then
	if [ -z "`(for packagestxt in $(find $DATA_DIR | grep PACKAGES.TXT); do cat $packagestxt | grep PACKAGE\ NAME: | sed -e s,\.tgz,, -e s,.*\ \ ,,; done) | grep -iw $(echo $DEP | sed 's,|.*,,')`" -a -z "`(for packagestxt in $(find $DATA_DIR | grep PACKAGES.TXT); do cat $packagestxt | grep PACKAGE\ NAME: | sed -e s,\.tgz,, -e s,.*\ \ ,,; done) | grep -iw $(echo $DEP | sed 's,.*|,,')`" ]; then
		echo "$DEP:unresolved"
	else
		echo "$DEP:resolved"
	fi
else
	if [ -z "`(for packagestxt in $(find $DATA_DIR | grep PACKAGES.TXT); do cat $packagestxt | grep PACKAGE\ NAME: | sed -e s,\.tgz,, -e s,.*\ \ ,,; done) | grep -iw $(echo $DEP | sed 's|>=.*||' | sed 's,|.*,,')`" -a -z "`(for packagestxt in $(find $DATA_DIR | grep PACKAGES.TXT); do cat $packagestxt | grep PACKAGE\ NAME: | sed -e s,\.tgz,, -e s,.*\ \ ,,; done) | grep -iw $(echo $DEP | sed 's|>=.*||' | sed 's,.*|,,')`" ]; then
		echo "$DEP:unresolved"
	else
		if [ $(echo `(for packagestxt in $(find $DATA_DIR | grep PACKAGES.TXT); do cat $packagestxt | grep PACKAGE\ NAME: | sed -e s,\.tgz,, -e s,.*\ \ ,,; done) | grep -iw $(echo $DEP | sed 's,|.*,,' | sed 's|>=.*||') | sed -e s,$(echo $DEP | sed 's,|.*,,' | sed 's|>=.*||' | sed 's| ||g')-,, | cut -d"-" -f1` `echo $DEP | sed 's,|.*,,' | cut -d" " -f3 | cut -d"-" -f1` | awk '{print ($1 >= $2) ? "0" : "1" }') == "0" ] || [ $(echo `(for packagestxt in $(find $DATA_DIR | grep PACKAGES.TXT); do cat $packagestxt | grep PACKAGE\ NAME: | sed -e s,\.tgz,, -e s,.*\ \ ,,; done) | grep -iw $(echo $DEP | sed 's,.*|,,' | sed 's|>=.*||') | sed -e s,$(echo $DEP | sed 's,.*|,,' | sed 's|>=.*||' | sed 's| ||g')-,, | cut -d"-" -f1` `echo $DEP | sed 's,.*| ,,' | cut -d" " -f3 | cut -d"-" -f1` | awk '{print ($1 >= $2) ? "0" : "1" }') == "0" ]; then
			echo "$DEP:resolved"
		else
			echo "$DEP:unresolved"
		fi
	fi
fi
}
